/**
	* \file			xbox_JoystickMouse.c
	* \brief		Implementation of the "Joystick & Mouse/Trackball" section of
	*           osdepend.h
  *
  * \note     Based off of XMAME source
	*/

//= I N C L U D E S ====================================================
#include "MAMEoX.h"
#include <assert.h>
#include <stdio.h>
#include "xbox_Direct3DRenderer.h"

#include "osd_cpu.h"
#include "osdepend.h"
#include "inptport.h"

#include "xbox_JoystickMouse.h"
#include "DebugLogger.h"

//= D E F I N E S ======================================================

//#define USE_HACKEDLIGHTGUN

  //! The value of the analog axis must exceed this deadzone
  //!  when being treated as a digital control (valid values are from [0,32767])
#define ANALOG_AS_DIGITAL_DEADZONE          (32768 * 0.33f)
#define ANALOG_BUTTON_AS_DIGITAL_DEADZONE   16
#define DELTA_AS_DIGITAL_DEADZONE			( char )( 2 )

BOOL                       m_SemiautoONE; // Semi Autofire enabled player 1
BOOL                       m_SemiautoTWO; // Semi Autofire enabled player 2
BOOL                       m_SemiautoTHREE; // Semi Autofire enabled player 3
BOOL                       m_SemiautoFOUR; // Semi Autofire enabled player 4
BOOL                       m_AutofireONE; // Autofire enabled player 1
BOOL                       m_AutofireTWO; // Autofire enabled player 2
BOOL                       m_AutofireTHREE; // Autofire enabled player 3
BOOL                       m_AutofireFOUR; // Autofire enabled player 4
BOOL                       m_FireONE; // Autofire firing player 1
BOOL                       m_FireTWO; // Autofire firing player 2
BOOL                       m_FireTHREE; // Autofire firing player 3
BOOL                       m_FireFOUR; // Autofire firing player 4

	// Analog stick deadzone
#define ANALOG_DEADZONE_PERCENTAGE      0.15f
#define ANALOG_DEADZONE				          (INT32)(32768.0f * ANALOG_DEADZONE_PERCENTAGE)
#define DEADZONE_RECTIFIER			        1.0f / (1.0f - ANALOG_DEADZONE_PERCENTAGE)

  // Used to set the temporary name variable in InitializeJoystickMouse
#define JOYNAME( _string__ )  sprintf( name, "%d\x10%s", stickIndex + 1, _string__ )

  // Helper macro, calls Helper_AddEntry in the InitializeJoystickMouse function
#define BEGINENTRYMAP()                                  UINT32 joycount = 0
#define ADDENTRY( _name__, _code__, _standardCode__ )    JOYNAME( _name__ ); Helper_AddEntry( name, (_code__), (_standardCode__), &joycount )

  //! The total number of JoystickInfo structures (= the total number of mapped buttons/axes on all joysticks)
#define MAX_JOYSTICKINFO_ENTRIES      128

//= G L O B A L = V A R S ==============================================
static struct JoystickInfo			g_joystickInfo[MAX_JOYSTICKINFO_ENTRIES] = {0};
static UINT32                   g_calibrationStep = 0;
static UINT32                   g_calibrationJoynum = 0;

  // number of OSD input keywords we've added (defined in xbox_Input.c)
extern UINT32										g_numOSDInputKeywords;


//= P R O T O T Y P E S ================================================
	//------------------------------------------------------------------
	//	Helper_AddEntry
  //! \brief    Adds an entry into the g_joystickInfo array
  //!
  //! \param    name - The user friendly name of the entry
  //! \param    code - The OSD-specific identifier of the entry
  //! \param    standardCode - The "standard" input code of the entry (see input.h in the MAME tree)
  //! \param    joycount - [IN,OUT] The index in the joystickInfo array to modify. Incremented on success
  //!
  //! \note     standardCode may be set to a value obtained via joyoscode_to_code for any codes 
  //!           w/out a specific entry in the keycode/joystick enum (defined in input.h of the MAME tree)
	//------------------------------------------------------------------
static void Helper_AddEntry( const char *name, INT32 code, INT32 standardCode, UINT32 *joycount );

static void Helper_CustomizeInputPortDefaults( struct ipd *defaults );
void InitializeJoystickMouse( void );



//= F U N C T I O N S ==================================================


/*
//---------------------------------------------------------------------
//	TerminateJoystickMouse
//---------------------------------------------------------------------
void TerminateJoystickMouse( void )
{
  UINT32 i = 0;
  while( g_joystickInfo[i].name )
  {
    free( g_joystickInfo[i].name );
    g_joystickInfo[i].name = NULL;
    ++i;
  }

  g_systemInitialized = FALSE;
}
*/

//---------------------------------------------------------------------
//	osd_get_joy_list
//---------------------------------------------------------------------
const struct JoystickInfo *osd_get_joy_list( void )
{
  //	return a list of all available joystick inputs (see input.h)
	return g_joystickInfo;
}

//---------------------------------------------------------------------
//	osd_is_joy_pressed
//---------------------------------------------------------------------
int osd_is_joy_pressed( int joycode )
{
	/*
		tell whether the specified joystick direction/button is pressed or not.
		joycode is the OS dependant code specified in the list returned by
		osd_get_joy_list().
	*/

	INT joyindex = JOYINDEX(joycode);
	INT joytype = JT(joycode);
	INT joynum = JOYNUM(joycode);

	const XINPUT_GAMEPAD *gamepad;
	const XINPUT_MOUSE *mouse;
	INT joyvalue = 0;
	
//	PRINTMSG(( T_TRACE, "osd_is_joy_pressed" ));

	mouse = GetMouseState( joynum );
	if (mouse)
	{
		PRINTMSG(( T_INFO, "osd_is_joy_pressed: mouse" ));
		switch( joytype )
		{
			case JT_MOUSE_BUTTON:
				switch( joyindex )
				{
					case BUTTON_MOUSE_LEFT:
						joyvalue = (mouse->bButtons & XINPUT_DEBUG_MOUSE_LEFT_BUTTON);
						break;
					case BUTTON_MOUSE_RIGHT:
						joyvalue = (mouse->bButtons & XINPUT_DEBUG_MOUSE_RIGHT_BUTTON);
						break;
					case BUTTON_MOUSE_MIDDLE:
						joyvalue = (mouse->bButtons & XINPUT_DEBUG_MOUSE_MIDDLE_BUTTON);
						break;
					case BUTTON_MOUSE_EXTRA1:
						joyvalue = (mouse->bButtons & XINPUT_DEBUG_MOUSE_XBUTTON1);
						break;
					case BUTTON_MOUSE_EXTRA2:
						joyvalue = (mouse->bButtons & XINPUT_DEBUG_MOUSE_XBUTTON2);
						break;
				}
				break;
			case JT_LSTICK_UP:
				joyvalue = (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE ) ?
					(mouse->cMickeysY <= -DELTA_AS_DIGITAL_DEADZONE ) :
					(mouse->cMickeysY >= DELTA_AS_DIGITAL_DEADZONE );
				break;
			case JT_LSTICK_DOWN:
				joyvalue =  (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE ) ?
					(mouse->cMickeysY >= DELTA_AS_DIGITAL_DEADZONE ):
					(mouse->cMickeysY <= -DELTA_AS_DIGITAL_DEADZONE );
				break;
			case JT_LSTICK_LEFT:
				joyvalue =  (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE ) ?
					(mouse->cMickeysX <= -DELTA_AS_DIGITAL_DEADZONE ) :
					(mouse->cMickeysX >= DELTA_AS_DIGITAL_DEADZONE);
				break;
			case JT_LSTICK_RIGHT:
				joyvalue =  (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE ) ?
					(mouse->cMickeysX >= DELTA_AS_DIGITAL_DEADZONE) :
					(mouse->cMickeysX <= -DELTA_AS_DIGITAL_DEADZONE );
				break;
			case JT_RSTICK_UP:
				joyvalue =  (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE ) ?
					(mouse->cWheel < 0 ) :
					(mouse->cWheel > 0 );
					//(mouse->cWheel <= -DELTA_AS_DIGITAL_DEADZONE ) :
					//(mouse->cWheel >= DELTA_AS_DIGITAL_DEADZONE );
				break;
			case JT_RSTICK_DOWN:
				joyvalue =  (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE ) ?
					(mouse->cWheel > 0 ) :
					(mouse->cWheel < 0 );
					//(mouse->cWheel >= DELTA_AS_DIGITAL_DEADZONE ) :
					//(mouse->cWheel <= -DELTA_AS_DIGITAL_DEADZONE );
				break;
		}

		PRINTMSG(( T_INFO, "osd_is_joy_pressed: joynum %lu, joyvalue %lu", joynum, joyvalue ));

		if (joyvalue > 0)
		{
			return joyvalue;
		}
	}


	gamepad = GetGamepadState( joynum );

	if( !gamepad )
	{
//		PRINTMSG(( T_ERROR, "Invalid joynum %lu!", joynum ));
		return 0;
	}

	if (joynum == 0) // Ken
	{

	if (gamepad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB && g_rendererOptions.m_autofiring == TRUE ) 
	{
		m_AutofireONE = FALSE;
		m_SemiautoONE = TRUE;
	}

	if (gamepad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB && g_rendererOptions.m_autofiring == TRUE )
	{
		m_SemiautoONE = FALSE;
		m_AutofireONE = TRUE;
	}
	if (gamepad->wButtons & XINPUT_GAMEPAD_BACK)   
	{
		m_AutofireONE = FALSE;
		m_SemiautoONE = FALSE;
	}



		switch( joytype )
		{
			case JT_BUTTON:  
				switch( joyindex )
				{
					case BUTTON_A:
						if ((m_AutofireONE == FALSE && m_SemiautoONE == FALSE) || g_rendererOptions.m_automaticfirea == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireONE == TRUE && g_rendererOptions.m_automaticfirea == TRUE )
						{
							if (m_FireONE == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireONE = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireONE == FALSE)
								{
									m_FireONE = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoONE)
						{
							if (m_FireONE == TRUE && g_rendererOptions.m_automaticfirea == TRUE )
								{
									m_FireONE = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireONE == FALSE)
								{
									m_FireONE = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}


					case BUTTON_X:
						if ((m_AutofireONE == FALSE && m_SemiautoONE == FALSE) || g_rendererOptions.m_automaticfirex == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireONE == TRUE && g_rendererOptions.m_automaticfirex == TRUE )
						{
							if (m_FireONE == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireONE = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireONE == FALSE)
								{
									m_FireONE = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoONE)
						{
							if (m_FireONE == TRUE && g_rendererOptions.m_automaticfirex == TRUE )
								{
									m_FireONE = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireONE == FALSE)
								{
									m_FireONE = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_B:
						if ((m_AutofireONE == FALSE && m_SemiautoONE == FALSE) || g_rendererOptions.m_automaticfireb == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireONE == TRUE && g_rendererOptions.m_automaticfireb == TRUE )
						{
							if (m_FireONE == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireONE = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireONE == FALSE)
								{
									m_FireONE = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoONE)
						{
							if (m_FireONE == TRUE && g_rendererOptions.m_automaticfireb == TRUE )
								{
									m_FireONE = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireONE == FALSE)
								{
									m_FireONE = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_Y:
						if ((m_AutofireONE == FALSE && m_SemiautoONE == FALSE) || g_rendererOptions.m_automaticfirey == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireONE == TRUE && g_rendererOptions.m_automaticfirey == TRUE )
						{
							if (m_FireONE == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireONE = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireONE == FALSE)
								{
									m_FireONE = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoONE)
						{
							if (m_FireONE == TRUE && g_rendererOptions.m_automaticfirey == TRUE )
								{
									m_FireONE = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireONE == FALSE)
								{
									m_FireONE = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_LEFT_TRIGGER:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_RIGHT_TRIGGER:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_START:
						return (gamepad->wButtons & XINPUT_GAMEPAD_START);
					case BUTTON_BACK:
						return (gamepad->wButtons & XINPUT_GAMEPAD_BACK);
					case BUTTON_WHITE:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_WHITE] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_BLACK:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_BLACK] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_LA:
						return (gamepad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB);
					case BUTTON_RA:
						return (gamepad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB);
				}
				break;


			case JT_DPAD_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
					}
			case JT_DPAD_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
					}
			case JT_DPAD_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP);
					}
			case JT_DPAD_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
					}






			case JT_LSTICK_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLY > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
				{
						return (gamepad->sThumbLX > ANALOG_AS_DIGITAL_DEADZONE );
		
				}
			case JT_LSTICK_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLY < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbLX < -ANALOG_AS_DIGITAL_DEADZONE );
					}

			case JT_LSTICK_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						
						return (gamepad->sThumbLX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbLY > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_LSTICK_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLX > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
				{
						return (gamepad->sThumbLY < -ANALOG_AS_DIGITAL_DEADZONE );
				}




			case JT_RSTICK_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRY > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRX > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRY < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRY > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRX > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRY < -ANALOG_AS_DIGITAL_DEADZONE );
					}


			}
		}


		if (joynum == 1) // Ken
	{

	if (gamepad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB && g_rendererOptions.m_autofiring == TRUE )  
	{
		m_AutofireTWO = FALSE;
		m_SemiautoTWO = TRUE;
	}

	if (gamepad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB && g_rendererOptions.m_autofiring == TRUE )
	{
		m_SemiautoTWO = FALSE;
		m_AutofireTWO = TRUE;
	}
	if (gamepad->wButtons & XINPUT_GAMEPAD_BACK)   
	{
		m_AutofireTWO = FALSE;
		m_SemiautoTWO = FALSE;
	}



		switch( joytype )
		{
			case JT_BUTTON:  
				switch( joyindex )
				{
					case BUTTON_A:
						if ((m_AutofireTWO == FALSE && m_SemiautoTWO == FALSE) || g_rendererOptions.m_automaticfirea == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireTWO == TRUE && g_rendererOptions.m_automaticfirea == TRUE )
						{
							if (m_FireTWO == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireTWO = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTWO == FALSE)
								{
									m_FireTWO = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoTWO)
						{
							if (m_FireTWO == TRUE && g_rendererOptions.m_automaticfirea == TRUE )
								{
									m_FireTWO = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTWO == FALSE)
								{
									m_FireTWO = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}


					case BUTTON_X:
						if ((m_AutofireTWO == FALSE && m_SemiautoTWO == FALSE) || g_rendererOptions.m_automaticfirex == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireTWO == TRUE && g_rendererOptions.m_automaticfirex == TRUE )
						{
							if (m_FireTWO == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireTWO = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTWO == FALSE)
								{
									m_FireTWO = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoTWO)
						{
							if (m_FireTWO == TRUE && g_rendererOptions.m_automaticfirex == TRUE )
								{
									m_FireTWO = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTWO == FALSE)
								{
									m_FireTWO = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_B:
						if ((m_AutofireTWO == FALSE && m_SemiautoTWO == FALSE) || g_rendererOptions.m_automaticfireb == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireTWO == TRUE && g_rendererOptions.m_automaticfireb == TRUE )
						{
							if (m_FireTWO == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireTWO = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTWO == FALSE)
								{
									m_FireTWO = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoTWO)
						{
							if (m_FireTWO == TRUE && g_rendererOptions.m_automaticfireb == TRUE )
								{
									m_FireTWO = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTWO == FALSE)
								{
									m_FireTWO = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_Y:
						if ((m_AutofireTWO == FALSE && m_SemiautoTWO == FALSE) || g_rendererOptions.m_automaticfirey == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireTWO == TRUE && g_rendererOptions.m_automaticfirey == TRUE )
						{
							if (m_FireTWO == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireTWO = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTWO == FALSE)
								{
									m_FireTWO = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoTWO)
						{
							if (m_FireTWO == TRUE && g_rendererOptions.m_automaticfirey == TRUE )
								{
									m_FireTWO = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTWO == FALSE)
								{
									m_FireTWO = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_LEFT_TRIGGER:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_RIGHT_TRIGGER:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_START:
						return (gamepad->wButtons & XINPUT_GAMEPAD_START);
					case BUTTON_BACK:
						return (gamepad->wButtons & XINPUT_GAMEPAD_BACK);
					case BUTTON_WHITE:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_WHITE] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_BLACK:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_BLACK] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_LA:
						return (gamepad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB);
					case BUTTON_RA:
						return (gamepad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB);
				}
				break;


			case JT_DPAD_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
					}
			case JT_DPAD_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
					}
			case JT_DPAD_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP);
					}
			case JT_DPAD_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
					}






			case JT_LSTICK_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLY > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
				{
						return (gamepad->sThumbLX > ANALOG_AS_DIGITAL_DEADZONE );
		
				}
			case JT_LSTICK_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLY < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbLX < -ANALOG_AS_DIGITAL_DEADZONE );
					}

			case JT_LSTICK_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						
						return (gamepad->sThumbLX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbLY > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_LSTICK_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLX > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
				{
						return (gamepad->sThumbLY < -ANALOG_AS_DIGITAL_DEADZONE );
				}




			case JT_RSTICK_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRY > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRX > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRY < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRY > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRX > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRY < -ANALOG_AS_DIGITAL_DEADZONE );
					}


			}
		}

		if (joynum == 2) // Ken
	{

	if (gamepad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB && g_rendererOptions.m_autofiring == TRUE ) 
	{
		m_AutofireTHREE = FALSE;
		m_SemiautoTHREE = TRUE;
	}

	if (gamepad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB && g_rendererOptions.m_autofiring == TRUE )
	{
		m_SemiautoTHREE = FALSE;
		m_AutofireTHREE = TRUE;
	}
	if (gamepad->wButtons & XINPUT_GAMEPAD_BACK)   
	{
		m_AutofireTHREE = FALSE;
		m_SemiautoTHREE = FALSE;
	}



		switch( joytype )
		{
			case JT_BUTTON:  
				switch( joyindex )
				{
					case BUTTON_A:
						if ((m_AutofireTHREE == FALSE && m_SemiautoTHREE == FALSE) || g_rendererOptions.m_automaticfirea == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireTHREE == TRUE && g_rendererOptions.m_automaticfirea == TRUE )
						{
							if (m_FireTHREE == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireTHREE = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTHREE == FALSE)
								{
									m_FireTHREE = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoTHREE)
						{
							if (m_FireTHREE == TRUE && g_rendererOptions.m_automaticfirea == TRUE )
								{
									m_FireTHREE = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTHREE == FALSE)
								{
									m_FireTHREE = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}


					case BUTTON_X:
						if ((m_AutofireTHREE == FALSE && m_SemiautoTHREE == FALSE) || g_rendererOptions.m_automaticfirex == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireTHREE == TRUE && g_rendererOptions.m_automaticfirex == TRUE )
						{
							if (m_FireTHREE == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireTHREE = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTHREE == FALSE)
								{
									m_FireTHREE = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoTHREE)
						{
							if (m_FireTHREE == TRUE && g_rendererOptions.m_automaticfirex == TRUE )
								{
									m_FireTHREE = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTHREE == FALSE)
								{
									m_FireTHREE = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_B:
						if ((m_AutofireTHREE == FALSE && m_SemiautoTHREE == FALSE) || g_rendererOptions.m_automaticfireb == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireTHREE == TRUE && g_rendererOptions.m_automaticfireb == TRUE )
						{
							if (m_FireTHREE == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireTHREE = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTHREE == FALSE)
								{
									m_FireTHREE = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoTHREE)
						{
							if (m_FireTHREE == TRUE && g_rendererOptions.m_automaticfireb == TRUE )
								{
									m_FireTHREE = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTHREE == FALSE)
								{
									m_FireTHREE = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_Y:
						if ((m_AutofireTHREE == FALSE && m_SemiautoTHREE == FALSE) || g_rendererOptions.m_automaticfirey == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireTHREE == TRUE && g_rendererOptions.m_automaticfirey == TRUE )
						{
							if (m_FireTHREE == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireTHREE = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTHREE == FALSE)
								{
									m_FireTHREE = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoTHREE)
						{
							if (m_FireTHREE == TRUE && g_rendererOptions.m_automaticfirey == TRUE )
								{
									m_FireTHREE = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireTHREE == FALSE)
								{
									m_FireTHREE = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_LEFT_TRIGGER:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_RIGHT_TRIGGER:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_START:
						return (gamepad->wButtons & XINPUT_GAMEPAD_START);
					case BUTTON_BACK:
						return (gamepad->wButtons & XINPUT_GAMEPAD_BACK);
					case BUTTON_WHITE:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_WHITE] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_BLACK:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_BLACK] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_LA:
						return (gamepad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB);
					case BUTTON_RA:
						return (gamepad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB);
				}
				break;


			case JT_DPAD_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
					}
			case JT_DPAD_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
					}
			case JT_DPAD_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP);
					}
			case JT_DPAD_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
					}






			case JT_LSTICK_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLY > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
				{
						return (gamepad->sThumbLX > ANALOG_AS_DIGITAL_DEADZONE );
		
				}
			case JT_LSTICK_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLY < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbLX < -ANALOG_AS_DIGITAL_DEADZONE );
					}

			case JT_LSTICK_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						
						return (gamepad->sThumbLX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbLY > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_LSTICK_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLX > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
				{
						return (gamepad->sThumbLY < -ANALOG_AS_DIGITAL_DEADZONE );
				}




			case JT_RSTICK_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRY > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRX > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRY < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRY > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRX > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRY < -ANALOG_AS_DIGITAL_DEADZONE );
					}


			}
		}
		if (joynum == 3)// Ken
	{

	if (gamepad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB && g_rendererOptions.m_autofiring == TRUE ) 
	{
		m_AutofireFOUR = FALSE;
		m_SemiautoFOUR = TRUE;
	}

	if (gamepad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB && g_rendererOptions.m_autofiring == TRUE )
	{
		m_SemiautoFOUR = FALSE;
		m_AutofireFOUR = TRUE;
	}
	if (gamepad->wButtons & XINPUT_GAMEPAD_BACK)   
	{
		m_AutofireFOUR = FALSE;
		m_SemiautoFOUR = FALSE;
	}



		switch( joytype )
		{
			case JT_BUTTON:  
				switch( joyindex )
				{
					case BUTTON_A:
						if ((m_AutofireFOUR == FALSE && m_SemiautoFOUR == FALSE) || g_rendererOptions.m_automaticfirea == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireFOUR == TRUE && g_rendererOptions.m_automaticfirea == TRUE )
						{
							if (m_FireFOUR == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireFOUR = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireFOUR == FALSE)
								{
									m_FireFOUR = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoFOUR)
						{
							if (m_FireFOUR == TRUE && g_rendererOptions.m_automaticfirea == TRUE )
								{
									m_FireFOUR = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireFOUR == FALSE)
								{
									m_FireFOUR = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}


					case BUTTON_X:
						if ((m_AutofireFOUR == FALSE && m_SemiautoFOUR == FALSE) || g_rendererOptions.m_automaticfirex == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireFOUR == TRUE && g_rendererOptions.m_automaticfirex == TRUE )
						{
							if (m_FireFOUR == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireFOUR = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireFOUR == FALSE)
								{
									m_FireFOUR = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoFOUR)
						{
							if (m_FireFOUR == TRUE && g_rendererOptions.m_automaticfirex == TRUE )
								{
									m_FireFOUR = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireFOUR == FALSE)
								{
									m_FireFOUR = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_B:
						if ((m_AutofireFOUR == FALSE && m_SemiautoFOUR == FALSE) || g_rendererOptions.m_automaticfireb == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireFOUR == TRUE && g_rendererOptions.m_automaticfireb == TRUE )
						{
							if (m_FireFOUR == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireFOUR = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireFOUR == FALSE)
								{
									m_FireFOUR = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoFOUR)
						{
							if (m_FireFOUR == TRUE && g_rendererOptions.m_automaticfireb == TRUE )
								{
									m_FireFOUR = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireFOUR == FALSE)
								{
									m_FireFOUR = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_Y:
						if ((m_AutofireFOUR == FALSE && m_SemiautoFOUR == FALSE) || g_rendererOptions.m_automaticfirey == FALSE )
						{
							return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
						}


						if (m_AutofireFOUR == TRUE && g_rendererOptions.m_automaticfirey == TRUE )
						{
							if (m_FireFOUR == TRUE)
								{
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
									{
									m_FireFOUR = FALSE;
									}
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireFOUR == FALSE)
								{
									m_FireFOUR = TRUE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
						}

						if (m_SemiautoFOUR)
						{
							if (m_FireFOUR == TRUE && g_rendererOptions.m_automaticfirey == TRUE )
								{
									m_FireFOUR = FALSE;
									return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
								}
							if (m_FireFOUR == FALSE)
								{
									m_FireFOUR = TRUE;
									if (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE)
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] < ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
									else
										{
											return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
										}
								}
						}
					case BUTTON_LEFT_TRIGGER:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_RIGHT_TRIGGER:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_START:
						return (gamepad->wButtons & XINPUT_GAMEPAD_START);
					case BUTTON_BACK:
						return (gamepad->wButtons & XINPUT_GAMEPAD_BACK);
					case BUTTON_WHITE:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_WHITE] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_BLACK:
						return (gamepad->bAnalogButtons[XINPUT_GAMEPAD_BLACK] > ANALOG_BUTTON_AS_DIGITAL_DEADZONE);
					case BUTTON_LA:
						return (gamepad->wButtons & XINPUT_GAMEPAD_LEFT_THUMB);
					case BUTTON_RA:
						return (gamepad->wButtons & XINPUT_GAMEPAD_RIGHT_THUMB);
				}
				break;


			case JT_DPAD_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
					}
			case JT_DPAD_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
					}
			case JT_DPAD_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_UP);
					}
			case JT_DPAD_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT);
					}
				else
					{
						return (gamepad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN);
					}






			case JT_LSTICK_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLY > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
				{
						return (gamepad->sThumbLX > ANALOG_AS_DIGITAL_DEADZONE );
		
				}
			case JT_LSTICK_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLY < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbLX < -ANALOG_AS_DIGITAL_DEADZONE );
					}

			case JT_LSTICK_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						
						return (gamepad->sThumbLX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbLY > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_LSTICK_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbLX > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
				{
						return (gamepad->sThumbLY < -ANALOG_AS_DIGITAL_DEADZONE );
				}




			case JT_RSTICK_UP:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRY > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRX > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_DOWN:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRY < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_LEFT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRX < -ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRY > ANALOG_AS_DIGITAL_DEADZONE );
					}
			case JT_RSTICK_RIGHT:
				if (g_rendererOptions.m_screenRotation == SR_0 || g_rendererOptions.m_rotationkeys == TRUE )
					{
						return (gamepad->sThumbRX > ANALOG_AS_DIGITAL_DEADZONE );
					}
				else
					{
						return (gamepad->sThumbRY < -ANALOG_AS_DIGITAL_DEADZONE );
					}


			}
		}
	
	return 0;
}


//---------------------------------------------------------------------
//	osd_is_joystick_axis_code
//---------------------------------------------------------------------
int osd_is_joystick_axis_code( int joycode )
{
/* added for building joystick seq for analog inputs */
//	PRINTMSG(( T_TRACE, "osd_is_joystick_axis_code" ));

	switch( JT( joycode ) )
	{
	case JT_LSTICK_UP:
	case JT_LSTICK_DOWN:
	case JT_LSTICK_LEFT:
	case JT_LSTICK_RIGHT:
	case JT_RSTICK_UP:
	case JT_RSTICK_DOWN:
	case JT_RSTICK_LEFT:
	case JT_RSTICK_RIGHT:
			return 1;

	case JT_BUTTON:
    {
        // Most of the XBOX buttons are actually
        // analog controls
      switch( JOYINDEX( joycode ) )
      {
      case BUTTON_A:
      case BUTTON_X:
      case BUTTON_B:
      case BUTTON_Y:
      case BUTTON_LEFT_TRIGGER:
      case BUTTON_RIGHT_TRIGGER:
      case BUTTON_WHITE:
      case BUTTON_BLACK:
        return 1;
      }
    }
    break;
  }
	return 0;
}

//---------------------------------------------------------------------
//	osd_joystick_needs_calibration
//---------------------------------------------------------------------
int osd_joystick_needs_calibration( void )
{
/* Joystick calibration routines BW 19981216 */
/* Do we need to calibrate the joystick at all? */

  // Disabled for now, calibration should be done
  // via the vastly superior LightgunCalibrator in
  // the ROM listing.
/*
  const XINPUT_CAPABILITIES *gp;
  UINT32 i = 0;

    // Search for subtype 'P' devices (guns)
  for( ; i < 4; ++i )
  {
    gp = GetGamepadCaps( 0 );  
    if( gp && gp->SubType == XINPUT_DEVSUBTYPE_GC_LIGHTGUN )
      return 1;
  }
*/
  return 0;
}


//---------------------------------------------------------------------
//	osd_joystick_start_calibration
//---------------------------------------------------------------------
void osd_joystick_start_calibration( void )
{
/* Preprocessing for joystick calibration. Returns 0 on success */
  const XINPUT_CAPABILITIES *gp;
  UINT32 i = 0;

  g_calibrationStep = 0;

    // Search for the first connected gun
  for( ; i < 4; ++i )
  {
    gp = GetGamepadCaps( 0 );  
    if( gp && gp->SubType == XINPUT_DEVSUBTYPE_GC_LIGHTGUN )
    {
      g_calibrationJoynum = i;
      return;
    }
  }
}

//---------------------------------------------------------------------
//	osd_joystick_calibrate_next
//---------------------------------------------------------------------
const char *osd_joystick_calibrate_next( void )
{
/* Prepare the next calibration step. Return a description of this step. */
/* (e.g. "move to upper left") */
  static char retString[128];

    // When we hit 2, switch over to the next gun to be calibrated,
    //  or return NULL to exit the process
  if( g_calibrationStep == 2 )
  {
    const XINPUT_CAPABILITIES *gp;
    ++g_calibrationJoynum;
    for( ; g_calibrationJoynum < 4; ++g_calibrationJoynum )
    {
      gp = GetGamepadCaps( g_calibrationJoynum );
      if( gp && gp->SubType == XINPUT_DEVSUBTYPE_GC_LIGHTGUN )
      {
          // Found another gun
        g_calibrationStep = 0;
        break;
      }
    }

    if( g_calibrationJoynum == 4 )
      return NULL;
  }

  sprintf( retString, "Gun %d: ", g_calibrationJoynum + 1 );
  switch( g_calibrationStep++ )
  {
  case 0:
    strcat( retString, "Upper left" );
    break;

  case 1:
    strcat( retString, "Center" );
    break;
  }

	return retString;
}

//---------------------------------------------------------------------
//	osd_joystick_calibrate
//---------------------------------------------------------------------
void osd_joystick_calibrate( void )
{
/* Get the actual joystick calibration data for the current position */

  if( g_calibrationStep && g_calibrationStep < 3 )
  {
	  const XINPUT_GAMEPAD *gp;
    if( (gp = GetGamepadState( g_calibrationJoynum )) )
    {
      g_calibrationData[g_calibrationJoynum].m_xData[g_calibrationStep-1] = gp->sThumbLX;
      g_calibrationData[g_calibrationJoynum].m_yData[g_calibrationStep-1] = gp->sThumbLY;
    }
    PRINTMSG(( T_INFO, "CALIB: STEP %d: %d, %d\n", g_calibrationStep - 1, gp->sThumbLX, gp->sThumbLY ));
  }
}

//---------------------------------------------------------------------
//	osd_joystick_end_calibration
//---------------------------------------------------------------------
void osd_joystick_end_calibration( void )
{

	{
	if ( m_AutofireONE == TRUE || m_SemiautoONE == TRUE || m_AutofireTWO == TRUE || m_SemiautoTWO == TRUE || m_AutofireTHREE == TRUE || m_SemiautoTHREE == TRUE || m_AutofireFOUR == TRUE || m_SemiautoFOUR == TRUE )
	{
	m_AutofireONE = FALSE;
	m_SemiautoONE = FALSE;
	m_AutofireTWO = FALSE;
	m_SemiautoTWO = FALSE;
	m_AutofireTHREE = FALSE;
	m_SemiautoTHREE = FALSE;
	m_AutofireFOUR = FALSE;
	m_SemiautoFOUR = FALSE;
	m_FireONE = FALSE;
	m_FireTWO = FALSE;
	m_FireTHREE = FALSE;
	m_FireFOUR = FALSE; // Ken
	}
}

/* Postprocessing (e.g. saving joystick data to config) */
}

//---------------------------------------------------------------------
//	osd_lightgun_read
//---------------------------------------------------------------------
void osd_lightgun_read(int player, int *deltax, int *deltay)
{
  assert( deltax && deltay );
#ifndef USE_HACKEDLIGHTGUN
  GetLightgunCalibratedPosition( player, deltax, deltay );
#else
  *deltax = *deltay = 0;
#endif
}

//---------------------------------------------------------------------
//	osd_trak_read
//---------------------------------------------------------------------
void osd_trak_read(int player, int *deltax, int *deltay)
{
	const XINPUT_MOUSE *m;

	assert( deltax && deltay );

	if ((m = GetMouseState( player )))
	{
		deltax = (int*)m->cMickeysX;
		deltay = (int*)m->cMickeysY;

		//if (deltax != 0 || deltay != 0)
		//{
		//	PRINTMSG(( T_INFO, "osd_trackread player %d, deltax %d, deltay %d\n", player, deltax, deltay ));
		//}
	}
	else
  /*
	const XINPUT_GAMEPAD *gp;

	if( (gp = GetGamepadState( player )) )
  {
      if( gp->sThumbLX > ANALOG_DEADZONE )
        *deltax = (int)((FLOAT)(gp->sThumbLX - ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
      else if( gp->sThumbLX < -ANALOG_DEADZONE )
        *deltax = (int)((FLOAT)(gp->sThumbLX + ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
      else
        *deltax = 0;

      if( gp->sThumbLY > ANALOG_DEADZONE )
        *deltay = (int)((FLOAT)(gp->sThumbLY - ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
      else if( gp->sThumbLY < -ANALOG_DEADZONE )
        *deltay = (int)((FLOAT)(gp->sThumbLY + ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
      else
        *deltay = 0;

      // Set to [-128,128]
    *deltax = ((FLOAT)*deltax * 128.0f / 32767.0f);
    *deltay = ((FLOAT)*deltay * 128.0f / 32767.0f);

      // Lock to the expected range
    if( *deltax > 128 )
      *deltax = 128;
    else if( *deltax < -128 )
      *deltax = -128;

    if( *deltay > 128 )
      *deltay = 128;
    else if( *deltay < -128 )
      *deltay = -128;
  }
  else  
  */
	  *deltax = *deltay = 0;
}


//---------------------------------------------------------------------
//	osd_analogjoy_read
//---------------------------------------------------------------------
void osd_analogjoy_read(	int player,
													int analog_axis[MAX_ANALOG_AXES], 
													InputCode analogjoy_input[MAX_ANALOG_AXES] )
{
	/* return values in the range -128 .. 128 (yes, 128, not 127) */
	UINT32 i = 0;
	FLOAT analogDivisor = 32767.0f;
	const XINPUT_GAMEPAD *gamepad;
	const XINPUT_CAPABILITIES *gpCaps;
	const XINPUT_MOUSE *mouse;
	INT joyvalue = 0;

//	PRINTMSG(( T_TRACE, "osd_analogjoy_read" ));

	gpCaps = GetGamepadCaps( player );
	gamepad = GetGamepadState( player );
	mouse = GetMouseState( player );

	if ( gamepad )
	{
		for(i = 0; i < MAX_ANALOG_AXES; ++i )
		{
			int joyindex, joytype, joynum;

			analog_axis[i] = 0;

			if (analogjoy_input[i] == CODE_NONE )
				continue;

			joyindex = JOYINDEX( analogjoy_input[i] );
			joytype = JT( analogjoy_input[i] );
			joynum = JOYNUM( analogjoy_input[i] );

				// Don't handle invalid controller IDs
			if( joynum >= 4 )
				continue;

		switch( joytype )
		{
		// *** JT_LSTICK_UP *** //
		case JT_LSTICK_UP:
	#ifdef USE_HACKEDLIGHTGUN
		if( gpCaps->SubType == XINPUT_DEVSUBTYPE_GC_LIGHTGUN )
		{
			INT32 junk;
			GetLightgunCalibratedPosition( joynum, &junk, &analog_axis[i] );
			analogDivisor = 128.0f;
		}
		else
	#endif
		{
			if( gamepad->sThumbLY > ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbLY - ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
			else if( gamepad->sThumbLY < -ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbLY + ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
			else
			analog_axis[i] = 0;

			// MAME seems to expect - values for Y axes to be "Up", so we need to
			// negate
			analog_axis[i] *= -1;
		}
		break;

		// *** JT_LSTICK_DOWN *** //
		case JT_LSTICK_DOWN:
	#ifdef USE_HACKEDLIGHTGUN
		if( gpCaps->SubType == XINPUT_DEVSUBTYPE_GC_LIGHTGUN )
		{
			INT32 junk;
			GetLightgunCalibratedPosition( joynum, &junk, &analog_axis[i] );
			analogDivisor = 128.0f;
		}
		else
	#endif
		{
			if( gamepad->sThumbLY > ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbLY - ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
			else if( gamepad->sThumbLY < -ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbLY + ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
			else
			analog_axis[i] = 0;

			// MAME seems to expect - values for Y axes to be "Up", so we need to
			// negate
			analog_axis[i] *= -1;
		}
				break;

		// *** JT_LSTICK_LEFT *** //
		case JT_LSTICK_LEFT:
	#ifdef USE_HACKEDLIGHTGUN
		if( gpCaps->SubType == XINPUT_DEVSUBTYPE_GC_LIGHTGUN )
		{
			INT32 junk;
			GetLightgunCalibratedPosition( joynum, &analog_axis[i], &junk );
			analogDivisor = 128.0f;
		}
		else
	#endif
		{
			if( gamepad->sThumbLX < -ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbLX + ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
			else if( gamepad->sThumbLX > ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbLX - ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
			else
			analog_axis[i] = 0;
		}
		break;

		// *** JT_LSTICK_RIGHT *** //
		case JT_LSTICK_RIGHT:
	#ifdef USE_HACKEDLIGHTGUN
		if( gpCaps->SubType == XINPUT_DEVSUBTYPE_GC_LIGHTGUN )
		{
			INT32 junk;
			GetLightgunCalibratedPosition( joynum, &analog_axis[i], &junk );
			analogDivisor = 128.0f;
		}
		else
	#endif
		{
			if( gamepad->sThumbLX < -ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbLX + ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
			else if( gamepad->sThumbLX > ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbLX - ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
			else
			analog_axis[i] = 0;
		}
		break;

		case JT_RSTICK_UP:
		case JT_RSTICK_DOWN:
		if( gamepad->sThumbRY > ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbRY - ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
		else if( gamepad->sThumbRY < -ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbRY + ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
		else if (mouse && mouse->cWheel != 0)
			analog_axis[i] = -mouse->cWheel;
		else
			analog_axis[i] = 0;

		// MAME seems to expect - values for Y axes to be "Up", so we need to
		// negate
		analog_axis[i] *= -1;
		break;

		case JT_RSTICK_LEFT:
		case JT_RSTICK_RIGHT:
		if( gamepad->sThumbRX < -ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbRX + ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
		else if( gamepad->sThumbRX > ANALOG_DEADZONE )
			analog_axis[i] = (int)((FLOAT)(gamepad->sThumbRX - ANALOG_DEADZONE) * DEADZONE_RECTIFIER);
		else
			analog_axis[i] = 0;
		break;

		case JT_BUTTON:
		{
			analogDivisor = 255.0f;
			// Most of the XBOX buttons are actually
			// analog controls, we map them from -128 to 128
			switch( joyindex )
			{
			case BUTTON_A:
			analog_axis[i] = (int)gamepad->bAnalogButtons[XINPUT_GAMEPAD_A] - 128;
			break;

			case BUTTON_X:
			analog_axis[i] = (int)gamepad->bAnalogButtons[XINPUT_GAMEPAD_X] - 128;
			break;

			case BUTTON_B:
			analog_axis[i] = (int)gamepad->bAnalogButtons[XINPUT_GAMEPAD_B] - 128;
			break;

			case BUTTON_Y:
			analog_axis[i] = (int)gamepad->bAnalogButtons[XINPUT_GAMEPAD_Y] - 128;
			break;

			case BUTTON_LEFT_TRIGGER:
			analog_axis[i] = (int)gamepad->bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] - 128;
			break;

			case BUTTON_RIGHT_TRIGGER:
			analog_axis[i] = (int)gamepad->bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] - 128;
			break;

			case BUTTON_WHITE:
			analog_axis[i] = (int)gamepad->bAnalogButtons[XINPUT_GAMEPAD_WHITE] - 128;
			break;

			case BUTTON_BLACK:
			analog_axis[i] = (int)gamepad->bAnalogButtons[XINPUT_GAMEPAD_BLACK] - 128;
			break;

			default:
			continue;
			}
		}

			// MAME seems to expect pedals to go from 
			// 128 (released) to -128 (pressed)
		analog_axis[i] *= -1;
		break;

			default:
				continue;
			}

			analog_axis[i] = ((FLOAT)analog_axis[i] * 128.0f / analogDivisor);
			if (analog_axis[i] < -128) 
				analog_axis[i] = -128;
			if (analog_axis[i] >  128) 
				analog_axis[i] =  128;
		}
	}


/*
	if (mouse)
	{
		PRINTMSG(( T_INFO, "osd_analogjoy_read: mouse" ));

		for(i = 0; i < MAX_ANALOG_AXES; ++i )
		{
			int joyindex, joytype, joynum;

			analog_axis[i] = 0;

			if (analogjoy_input[i] == CODE_NONE )
				continue;

			joyindex = JOYINDEX( analogjoy_input[i] );
			joytype = JT( analogjoy_input[i] );
			joynum = JOYNUM( analogjoy_input[i] );

				// Don't handle invalid controller IDs
			if( joynum >= 4 )
				continue;

			switch( joytype )
			{
				case JT_LSTICK_UP:
					joyvalue = mouse->cMickeysY;
					if (joyvalue <= -DELTA_AS_DIGITAL_DEADZONE)
						analog_axis[i] = joyvalue;
					//analog_axis[i] = mouse->cMickeysY;
					break;
				case JT_LSTICK_DOWN:
					joyvalue = mouse->cMickeysY;
					if (joyvalue >= DELTA_AS_DIGITAL_DEADZONE)
						analog_axis[i] = joyvalue;
					//analog_axis[i] = -mouse->cMickeysY;
					break;
				case JT_LSTICK_LEFT:
					joyvalue = mouse->cMickeysX;
					if (joyvalue <= -DELTA_AS_DIGITAL_DEADZONE)
						analog_axis[i] = joyvalue;
					//analog_axis[i] = -mouse->cMickeysX;
					break;
				case JT_LSTICK_RIGHT:
					joyvalue = mouse->cMickeysX;
					if (joyvalue >= DELTA_AS_DIGITAL_DEADZONE)
						analog_axis[i] = joyvalue;
					//analog_axis[i] = mouse->cMickeysX;
					break;
				case JT_RSTICK_UP:	
					joyvalue = mouse->cWheel;
					if (joyvalue <= -DELTA_AS_DIGITAL_DEADZONE)
						analog_axis[i] = joyvalue;
					//analog_axis[i] = -(int)mouse->cWheel;
					break;
				case JT_RSTICK_DOWN:
					joyvalue = mouse->cWheel;
					if (joyvalue >= DELTA_AS_DIGITAL_DEADZONE)
						analog_axis[i] = joyvalue;
					//analog_axis[i] = (int)mouse->cWheel;
					break;
				
				default:
					continue;
			}
		}
	}
*/
}

#pragma code_seg( "MXCINPUT" )
#pragma data_seg( "MXDINPUT" )
#pragma comment(linker, "/merge:MXCINPUT=MOXINPUT")
#pragma comment(linker, "/merge:MXDINPUT=MOXINPUT")


//---------------------------------------------------------------------
//	InitializeJoystickMouse
//---------------------------------------------------------------------
void InitializeJoystickMouse( void )
{
  BEGINENTRYMAP();
  INT32 stickIndex = 0;

	for( ; stickIndex < 4; ++stickIndex )
	{
    char name[32];
    

      // Note on "CODE_OTHER" entries:
      // The way that internal_code_find_joystick() (input.c) works is to check the standard
      // code against CODE_OTHER, and then the "oscode" against whatever we set in the second parameter
      // to the ADDENTRY macro. However, other places in input.c require us to have a valid 
      // joyoscode_to_code() mapping for each "CODE_OTHER" entry. Therefore, we do an AXISCODE or
      // BUTTONCODE just before the ADDENTRY call. This allows us to use the joycode_to_oscode() mapping
      // to assign buttons to functions in osd_customize_inputport_defaults() below, while retaining
      // the naming functionality in the UI "Input" menu. If we don't set the syscode (param 3 of AXISCODE)
      // to CODE_OTHER, any extended entries will be named "n/a", which isn't such a great user experience

      // DPad
    ADDENTRY( "D-Pad\x18",      JOYCODE( stickIndex, JT_DPAD_UP, 0 ),        STDCODE( UP ) );
    ADDENTRY( "D-Pad\x1a",   JOYCODE( stickIndex, JT_DPAD_RIGHT , 0 ),    STDCODE( RIGHT ) );
    ADDENTRY( "D-Pad\x19",    JOYCODE( stickIndex, JT_DPAD_DOWN, 0 ),      STDCODE( DOWN ) );
    ADDENTRY( "D-Pad\x1b",    JOYCODE( stickIndex, JT_DPAD_LEFT , 0 ),     STDCODE( LEFT ) );

      // Left analog
    AXISCODE( stickIndex, JT_LSTICK_UP );
    AXISCODE( stickIndex, JT_LSTICK_RIGHT );
    AXISCODE( stickIndex, JT_LSTICK_DOWN );
    AXISCODE( stickIndex, JT_LSTICK_LEFT );
    ADDENTRY( "L-Stick\x18",        JOYCODE( stickIndex, JT_LSTICK_UP, 0 ),     CODE_OTHER );
    ADDENTRY( "L-Stick\x1a",     JOYCODE( stickIndex, JT_LSTICK_RIGHT , 0 ), CODE_OTHER );
    ADDENTRY( "L-Stick\x19",      JOYCODE( stickIndex, JT_LSTICK_DOWN, 0 ),   CODE_OTHER );
    ADDENTRY( "L-Stick\x1b",      JOYCODE( stickIndex, JT_LSTICK_LEFT , 0 ),  CODE_OTHER );

      // Right analog
    AXISCODE( stickIndex, JT_RSTICK_UP );
    AXISCODE( stickIndex, JT_RSTICK_RIGHT );
    AXISCODE( stickIndex, JT_RSTICK_DOWN );
    AXISCODE( stickIndex, JT_RSTICK_LEFT );
    ADDENTRY( "R-Stick\x18",        JOYCODE( stickIndex, JT_RSTICK_UP, 0 ),     CODE_OTHER );
    ADDENTRY( "R-Stick\x1a",     JOYCODE( stickIndex, JT_RSTICK_RIGHT , 0 ), CODE_OTHER );
    ADDENTRY( "R-Stick\x19",      JOYCODE( stickIndex, JT_RSTICK_DOWN, 0 ),   CODE_OTHER );
    ADDENTRY( "R-Stick\x1b",      JOYCODE( stickIndex, JT_RSTICK_LEFT , 0 ),  CODE_OTHER );

      // Buttons
    BUTTONCODE( stickIndex, BUTTON_WHITE );
    BUTTONCODE( stickIndex, BUTTON_BLACK );
    BUTTONCODE( stickIndex, BUTTON_LA );
    BUTTONCODE( stickIndex, BUTTON_RA );
    ADDENTRY( "A",            JOYCODE( stickIndex, JT_BUTTON, BUTTON_A ),              STDCODE( BUTTON1 ) );
    ADDENTRY( "X",            JOYCODE( stickIndex, JT_BUTTON, BUTTON_X ),              STDCODE( BUTTON2 ) );
    ADDENTRY( "B",            JOYCODE( stickIndex, JT_BUTTON, BUTTON_B ),              STDCODE( BUTTON3 ) );
    ADDENTRY( "Y",            JOYCODE( stickIndex, JT_BUTTON, BUTTON_Y ),              STDCODE( BUTTON4 ) );
    ADDENTRY( "L-Trigger",        JOYCODE( stickIndex, JT_BUTTON, BUTTON_LEFT_TRIGGER ),   STDCODE( BUTTON5 ) );
    ADDENTRY( "R-Trigger",        JOYCODE( stickIndex, JT_BUTTON, BUTTON_RIGHT_TRIGGER ),  STDCODE( BUTTON6 ) );
    ADDENTRY( "Start",        JOYCODE( stickIndex, JT_BUTTON, BUTTON_START ),          STDCODE( START ) );
    ADDENTRY( "Back",         JOYCODE( stickIndex, JT_BUTTON, BUTTON_BACK ),           STDCODE( SELECT ) );
    ADDENTRY( "White",        JOYCODE( stickIndex, JT_BUTTON, BUTTON_WHITE ),          CODE_OTHER );
    ADDENTRY( "Black",        JOYCODE( stickIndex, JT_BUTTON, BUTTON_BLACK ),          CODE_OTHER );
    ADDENTRY( "L-Stick Click",           JOYCODE( stickIndex, JT_BUTTON, BUTTON_LA ),             CODE_OTHER );
    ADDENTRY( "R-Stick Click",           JOYCODE( stickIndex, JT_BUTTON, BUTTON_RA ),             CODE_OTHER );

	  // Mouse Buttons
    ADDENTRY( "L-Mouse",			JOYCODE( stickIndex, JT_MOUSE_BUTTON, BUTTON_MOUSE_LEFT ),		MOUSECODE (BUTTON1) );
    ADDENTRY( "R-Mouse",		JOYCODE( stickIndex, JT_MOUSE_BUTTON, BUTTON_MOUSE_RIGHT ),		MOUSECODE (BUTTON2) );
    ADDENTRY( "C-Mouse",		JOYCODE( stickIndex, JT_MOUSE_BUTTON, BUTTON_MOUSE_MIDDLE ),	MOUSECODE (BUTTON3) );
    ADDENTRY( "X1-Mouse",			JOYCODE( stickIndex, JT_MOUSE_BUTTON, BUTTON_MOUSE_EXTRA1 ),	MOUSECODE (BUTTON4) );
    ADDENTRY( "X2-Mouse",			JOYCODE( stickIndex, JT_MOUSE_BUTTON, BUTTON_MOUSE_EXTRA2 ),	MOUSECODE (BUTTON5) );
  }
}

//-------------------------------------------------------
//	Helper_AddEntry
//-------------------------------------------------------
static void Helper_AddEntry( const char *name, INT32 code, INT32 standardCode, UINT32 *joycount )
{
  struct JoystickInfo *ji = NULL;
	struct ik *inputkeywords;

  assert( joycount && *joycount < MAX_JOYSTICKINFO_ENTRIES );

  ji = &g_joystickInfo[*joycount];

  ji->name = strdup( name );
  if( !ji->name )
  {
    PRINTMSG(( T_ERROR, "Out of memory creating joystick entry!" ));
    osd_print_error( "Out of memory!" );
    return;
  }

    // Convert spaces in ji->name to '_'
  {
    char *cur = ji->name;
    while( *cur )
    {
      if( *cur == ' ' )
        *cur = ' ';
      ++cur;
    }
  }

  ji->code = code;
  ji->standardcode = standardCode;

    // Reallocate the osd_input_keywords array, and add the new entry
	inputkeywords = (struct ik*)osd_realloc( osd_input_keywords, (g_numOSDInputKeywords + 2) * sizeof(struct ik) );
  if( inputkeywords )
  {
    osd_input_keywords = inputkeywords;
    osd_input_keywords[g_numOSDInputKeywords].name = ji->name;  // Just share the value
    osd_input_keywords[g_numOSDInputKeywords].type = IKT_OSD_JOY;
    osd_input_keywords[g_numOSDInputKeywords].val = code;
    ++g_numOSDInputKeywords;

      // Tag the end of the list
		osd_input_keywords[g_numOSDInputKeywords].name = NULL;
    ++(*joycount);
  }
}

#pragma code_seg()
#pragma data_seg()



